<template>
  <view class="login">
    <image :src="vuex_imgPath+'/login_bg.png'" class="login-bg" />
    <view class="logo">
      <image :src="vuex_imgPath+'/newLogo.png'" />
    </view>
    <view class="welcome">
      <view class="text01">Hello</view>
      <view class="text02">欢迎使用鑫方盛</view>
    </view>
    <view class="login-btn">
      <!-- #ifdef MP-WEIXIN -->
      <button type="default" class="login-box login-org btn-noborder" :plain="true" @click="onLoginButtonClick()">手机号快捷登录</button>
      <!-- #endif -->
      <!-- #ifdef MP-ALIPAY -->
      <button type="default" class="login-box login-org btn-noborder" :plain="true" @click="onLoginButtonClick()">支付宝一键登录</button>
      <!-- #endif -->
      <!-- #ifdef H5 -->
      <!-- 暂时先注掉 极光一键登录后期需求调整 <button type="default" class="login-box login-org btn-noborder" :plain="true" @click="phoneLoginFn">手机号一键登录</button> -->
      <!-- #endif -->
    </view>
    <view class="privacy-radio">
      <u-checkbox-group v-model="flag" placement="row">
        <u-checkbox
          shape="circle"
          active-color="#FF5533"
          name="true"
        />
      </u-checkbox-group>
      <view class="radio-hint">
        我已阅读井同意
        <view @click="goWeb('agree')">《鑫方盛服务协议》</view>和
        <view @click="goWeb('pol')">《隐私政策》</view>
      </view>
    </view>
    <view class="login-other">
      <view v-if="isWeixin" class="login-box login-weixin" @click="onH5WeixinButtonClick()"><text class="font_family icon-WeChat_tongyong_1" />微信登录</view>
      <view class="login-box login-zh" @click="goPasswordLoginFn()"><text class="font_family icon-person_tongyong_1"></text>账号密码</view>
      <view class="login-box login-sj" @click="goCodeLogin()"><text class="font_family icon-a-mobilephone_tongyong_1"></text>验证码登录</view>
      <view class="hint">首次登录会自动注册新账号</view>
    </view>
    <u-popup :show="showPopVisible" :round="5" mode="bottom" height="420" @close="closePop">
      <RegisterPop
        :page-type="type"
        :callback-url="callbackUrl"
        :url-params="urlParams"
        :branch-data="registParams"
        @closePopFn="closePop"
      />
    </u-popup>
  </view>
</template>

<script>
import RegisterPop from '@/components/register-pop';
import { loginMixin } from '@/login/loginMixin';
import { mapInfo } from '@/common/getMapInfosMixin';
import { JVinit } from '@/utils/loginSdk';
import '@/static/scss/login.scss';
import { tdkUrl } from '@/common/common'

export default {
  name: 'CodeLogin',
  components: {
    RegisterPop
  },
  mixins: [loginMixin, mapInfo],
  data() {
    return {
      flag: false,
      showPopVisible: false,
      type: '',
      callbackUrl: '',
      urlParams: '',
      registParams: '',
      isWeixin: false,
    }
  },
  onShow() {
    // #ifdef MP-ALIPAY
    if (uni.getStorageSync('inviteCode')) {
      this.callbackUrl = '/shopIndex/shopIndex' //扫门店二维码注册跳转云采首页
      this.type = 'ground'
    }
    // #endif
  },
  onLoad(option) {
    console.log(4111111111,option.shareMemberId)
    //#ifdef H5
    this.isWeixin = this.isWechat()
    // #endif
    if (!this.vuex_cityInfo || JSON.stringify(this.vuex_cityInfo) == '{}') { // 没有本地信息，则提示授权
      uni.getSetting({
        success: (res) => {
          if (!res.authSetting['scope.userLocation']) {
            this.getLocation()
          } else {
            this.getLocation()
          }
        }
      })
    }

    if (this.vuex_loginStatus) {
      if (this.vuex_memberInfo && (this.vuex_memberInfo.accountType == 20 || this.vuex_memberInfo.accountType == 30)) {
       console.log('登录跳转门店首页排查22222',this.vuex_memberInfo)
        this.callbackUrl = '/shopIndex/shopIndex'
        uni.redirectTo({ url: this.callbackUrl })
      } else {
          console.log('登录跳转门店首页排查22222',this.vuex_memberInfo)
        uni.redirectTo({ url: '/pages/index/index' })
      }
    }

    const { type = '', callbackUrl = '', urlParams = '', registParams = '' } = option
    this.type = type
    this.callbackUrl = callbackUrl
    this.urlParams = urlParams
    this.registParams = registParams ? JSON.parse(registParams) : ''
        console.log(this.urlParams )
    // #ifdef MP-WEIXIN
    if (option.q) {
      const src = decodeURIComponent(option.q)
      if (src.includes('inviteCode')) {
        const inviteCode = src.match(/inviteCode=(\S*)/)[1]
        this.callbackUrl = '/shopIndex/shopIndex' //扫门店二维码注册跳转云采首页
        this.type = 'ground'
        console.log('登录跳转门店邀请码',option.q,inviteCode)
        uni.setStorageSync('inviteCode', inviteCode)
      }
    }
    // #endif
  },
  methods: {
    goWeb(type) {
      if (type == 'pol') {
        uni.navigateTo({ url: `./pol` })
      } else {
        uni.navigateTo({ url: `./agree` })
      }
    },
    onLoginButtonClick() {
      if (!this.flag) {
        this.$u.toast('未勾选协议，请勾选')
        return
      }
      this.loginEvent(this.registParams, this.type, this.callbackUrl, this.urlParams ? JSON.parse(this.urlParams) : null);
    },
    //m站 微信网页授权登录
    onH5WeixinButtonClick() {
      if (!this.flag) {
        this.$u.toast('未勾选协议，请勾选')
        return
      }
      window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc8374f608503bb7a&redirect_uri=' + tdkUrl.develop + '/front/pages/myAccount/index&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
    },
    //方法：用来判断是否是微信内置的浏览器
    isWechat() {
      return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
    },
    // m站 手机号极光一键登录
    phoneLoginFn() {
      if (!this.flag) {
        this.$u.toast('未勾选协议，请勾选')
        return
      }
      JVinit()
    },
    goCodeLogin() {
    if(this.type == 'pointsGoodsDetail'){
      const urlParams ={
        skuId:JSON.parse(this.urlParams).skuId,
        title:JSON.parse(this.urlParams).title,
        exchangeType:JSON.parse(this.urlParams).exchangeType
      }
      uni.navigateTo({
        url: `./login?type=${this.type}&callbackUrl=${this.callbackUrl}&urlParams=${JSON.stringify(urlParams)}&registParams=${JSON.stringify(this.registParams)}`
      })
    }else{
      uni.navigateTo({
        url: `./login?type=${this.type}&callbackUrl=${this.callbackUrl}&urlParams=${this.urlParams}&registParams=${JSON.stringify(this.registParams)}`
      })
    }

    },
    goPasswordLoginFn() {
      console.log(this.urlParams)
      if(this.type == 'pointsGoodsDetail'){
        const urlParams ={
          skuId:JSON.parse(this.urlParams).skuId,
          title:JSON.parse(this.urlParams).title,
          exchangeType:JSON.parse(this.urlParams).exchangeType
        }
        uni.navigateTo({
          url: `./passwordLogin?type=${this.type}&callbackUrl=${this.callbackUrl}&urlParams=${JSON.stringify(urlParams)}`
        })
        }else{
        uni.navigateTo({
          url: `./passwordLogin?type=${this.type}&callbackUrl=${this.callbackUrl}&urlParams=${this.urlParams}`
        })
        }


    },
  }
}
</script>

<style scoped lang="scss">
.login{
  .logo{
    image{
      width:324rpx;
      height:74rpx;
    }
  }
  .welcome{
    padding: 156rpx 0 200rpx 2rpx;
    .text01{
      font-size: 48rpx;
      font-weight: bold;
    }
    .text02{
      font-size:40rpx;
    }
  }
}
</style>
